package com.itheima.reggie.common;/**
 * 功能描述
 *
 * @author 唯硕
 * @date 2022/09/20  17:29
 */

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 *@ClassName MyMetaObjectMapper
 *@Description TODO 自定义原数据对象处理器   实现Mybatis Plus公共字段的属性自动填充
 *@Author 唯硕
 *@Date 2022/9/20 17:29
 *@Version 1.0
 */
@Component
@Slf4j
public class MyMetaObjectMapper implements MetaObjectHandler {

//    插入操作自动填充
    @Override
    public void insertFill(MetaObject metaObject) {
       metaObject.setValue("createTime", LocalDateTime.now());
       metaObject.setValue("updateTime", LocalDateTime.now());
        Long currentId = BaseContext.getCurrentId();

        metaObject.setValue("createUser",currentId);
       metaObject.setValue("updateUser",currentId);
    }


//    更新修改操作自动填充
    @Override
    public void updateFill(MetaObject metaObject) {
        long id = Thread.currentThread().getId();
        log.info("线程id为{}",id);
        Long currentId = BaseContext.getCurrentId();

        metaObject.setValue("updateTime", LocalDateTime.now());

        metaObject.setValue("updateUser",currentId);

    }
}
